home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / ModelClip / ModelClipTest2.java.z / ModelClipTest2.java
Encoding:
Java Source  |  2003-08-08  |  6.1 KB  |  191 lines

  1. /*
  2.  *    @(#)ModelClipTest2.java 1.10 02/10/21 13:45:04
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.utils.behaviors.mouse.*;
  41. import com.sun.j3d.utils.geometry.Cylinder;
  42. import com.sun.j3d.utils.geometry.Box;
  43. import java.applet.Applet;
  44. import java.awt.*;
  45. import java.awt.event.*;
  46. import com.sun.j3d.utils.applet.MainFrame;
  47. import com.sun.j3d.utils.universe.*;
  48. import javax.media.j3d.*;
  49. import javax.vecmath.*;
  50.  
  51. /**
  52.  * ModelClipTest2 draws a cylinder and creates two clip planes
  53.  * to see the interior of the cylinder. It also has a behavior to
  54.  * move the clip planes.
  55.  */
  56. public class ModelClipTest2 extends Applet {
  57.  
  58.     private SimpleUniverse u;
  59.     
  60.   public BranchGroup createSceneGraph()
  61.   {
  62.     // Create the root of the branch graph
  63.     BranchGroup objRoot = new BranchGroup();
  64.  
  65.     // Create a Transformgroup to scale all objects so they
  66.     // appear in the scene.
  67.     TransformGroup objScale = new TransformGroup();
  68.     Transform3D t3d = new Transform3D();
  69.     t3d.setScale(0.4);
  70.     objScale.setTransform(t3d);
  71.     objRoot.addChild(objScale);
  72.  
  73.     // Create lights
  74.     BoundingSphere bounds =
  75.       new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  76.     
  77.     //Shine it with two colored lights.
  78.     Color3f lColor0 = new Color3f(1.0f, 1.0f, 1.0f);
  79.     Color3f lColor1 = new Color3f(0.5f, 0.0f, 0.5f);
  80.     Color3f lColor2 = new Color3f(0.7f, 0.7f, 0.0f);
  81.     Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, 1.0f);
  82.     Vector3f lDir2  = new Vector3f(0.0f, 0.0f, -1.0f);
  83.     
  84.     AmbientLight     lgt0 = new AmbientLight(true, lColor2);
  85.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  86.     DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
  87.     lgt0.setInfluencingBounds(bounds);
  88.     lgt1.setInfluencingBounds(bounds);
  89.     lgt2.setInfluencingBounds(bounds);
  90.     objScale.addChild(lgt0);
  91.     objScale.addChild(lgt1);
  92.     objScale.addChild(lgt2);
  93.  
  94.     // Create a Transformgroup for the geometry
  95.     TransformGroup objRot = new TransformGroup();
  96.     Transform3D t3d1 = new Transform3D();
  97.     AxisAngle4f rot1 = new AxisAngle4f(0.0f, 1.0f, 0.0f, 45.0f);
  98.     t3d1.setRotation(rot1);
  99.     objRot.setTransform(t3d1);
  100.     objScale.addChild(objRot);
  101.  
  102.    
  103.     //Create a cylinder
  104.     PolygonAttributes attr = new PolygonAttributes();
  105.     attr.setCullFace(PolygonAttributes.CULL_NONE);
  106.     Appearance ap = new Appearance();
  107.     Material mat = new Material();
  108.     mat.setLightingEnable(true);
  109.     ap.setMaterial(mat);
  110.     ap.setPolygonAttributes(attr);
  111.  
  112.     Cylinder CylinderObj = new Cylinder(0.5f, 2.2f, ap);
  113.     objRot.addChild(CylinderObj);
  114.     
  115.     //Create a box
  116.     Box BoxObj = new Box(0.8f, 0.8f, 0.8f, ap);
  117.     objRot.addChild(BoxObj);
  118.  
  119.  
  120.     // This Transformgroup is used by the mouse manipulators to
  121.     // move the model clip planes.
  122.     TransformGroup objTrans = new TransformGroup();
  123.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  124.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  125.     objRot.addChild(objTrans);
  126.  
  127.     // Create the rotate behavior node
  128.     MouseRotate behavior = new MouseRotate(objTrans);
  129.     objTrans.addChild(behavior);
  130.     behavior.setSchedulingBounds(bounds);
  131.     
  132.     // Create the zoom behavior node
  133.     MouseZoom behavior2 = new MouseZoom(objTrans);
  134.     objTrans.addChild(behavior2);
  135.     behavior2.setSchedulingBounds(bounds);
  136.     
  137.     //Create Model Clip
  138.     ModelClip mc = new ModelClip();
  139.     boolean enables[] = {false, false, false, false, false, false};
  140.     Vector4d eqn = new Vector4d(0.0, 1.0, 1.0, 0.0);
  141.     mc.setEnables(enables);
  142.     mc.setPlane(1, eqn);
  143.     mc.setEnable(1, true);
  144.     mc.setInfluencingBounds(bounds);
  145.     objTrans.addChild(mc);
  146.  
  147.  
  148.     // Let Java 3D perform optimizations on this scene graph.
  149.     objRoot.compile();
  150.  
  151.     return objRoot;
  152.   }
  153.   
  154.   public ModelClipTest2 (){
  155.   }
  156.  
  157.     public void init() {
  158.     setLayout(new BorderLayout());
  159.     GraphicsConfiguration config =
  160.         SimpleUniverse.getPreferredConfiguration();
  161.  
  162.     Canvas3D c = new Canvas3D(config);
  163.     add("Center", c);
  164.     
  165.     // Create a simple scene and attach it to the virtual universe
  166.     BranchGroup scene = createSceneGraph();
  167.     u = new SimpleUniverse(c);
  168.  
  169.     // This will move the ViewPlatform back a bit so the
  170.     // objects in the scene can be viewed.
  171.     u.getViewingPlatform().setNominalViewingTransform();
  172.     
  173.     u.addBranchGraph(scene);
  174.     }
  175.  
  176.     public void destroy() {
  177.     u.cleanup();
  178.     }
  179.  
  180.   
  181.   
  182.   public static void main(String argv[])
  183.   {
  184.     
  185.     BranchGroup group;
  186.     
  187.     new MainFrame(new ModelClipTest2(), 500, 500);
  188.   }
  189. }
  190.  
  191.